<script setup lang="ts">
import useDragramStore from '../../store/useDragramStore'
import FileTreeDialog from './components/FileTreeDialog.vue'
const dragramStore = useDragramStore()

const { fileName, searchFormItems } = storeToRefs(dragramStore)
const reset = () => {
  window.location.reload()
}

const [fileTreeToggle, handleFileTreeDialog] = useToggle(false)

const FileInputRef = ref()
const applyToWorkspace = async () => {
  if (!fileName.value) {
    // ElMessage.error('请输入页面名称,短横线命名法,例如:home-page')
    ElNotification({
      title: '⚠️警告',
      message: '请输入页面名称,短横线命名法,例如:home-page',
      type: 'warning',
      duration: 3000
    })
    // 输入框聚焦
    FileInputRef.value.focus()
  } else if (searchFormItems.value.length === 0) {
    ElMessage.warning({
      message: '检索区域内容为空',
      grouping: true,
      showClose: true
    })
  } else {
    handleFileTreeDialog()
  }
}
</script>

<template>
  <header class="header-wrapper">
    <h1 class="w-[220px] flex justify-center items-center font-bold text-2xl">
      <el-icon><ArrowLeftBold /></el-icon><nuxt-link to="/">返回首页</nuxt-link>
    </h1>
    <div class="flex items-center">
      <div class="flex items-end text-xl">
        <el-input
          ref="FileInputRef"
          class="filename-input"
          v-model="fileName"
          placeholder="请输入页面名称"
          clearable
        ></el-input>
      </div>
    </div>
    <div class="flex items-center pr-10">
      <el-button type="primary" icon="RefreshLeft" @click="reset">重置</el-button>
      <el-button type="success" icon="ChromeFilled" @click="applyToWorkspace"
        >应用</el-button
      >
      <el-button type="success" icon="Download" @click="dragramStore.down"
        >下载</el-button
      >
    </div>
    <FileTreeDialog v-model="fileTreeToggle" />
  </header>
</template>

<style lang="scss" scoped>
.header-wrapper {
  display: flex;
  border-bottom: 1px solid #dcdfe6;
  justify-content: space-between;
  .filename-input {
    width: 300px;
    height: 40px;
    border-bottom: 2px solid var(--el-color-primary);
    :deep(.el-input__wrapper) {
      outline: none;
      box-shadow: none;
      background-color: var(--primary-bg-color);
    }
    :deep(.el-input__inner) {
      height: 100%;
      font-size: 18px;
      color: #000;
    }
  }
}
</style>
